package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> getSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.getSetmealPage(setmealPageQueryDTO);
        return Result.success("", pageResult);
    }

    @PostMapping()
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<PageResult> save(@RequestBody SetmealDTO setmealDTO) {
        log.info("分页查询:{}", setmealDTO);
        setmealService.saveSetmeal(setmealDTO);
        return Result.success("添加成功", null);
    }

    @PutMapping()
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> updateWithStDish(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐:{}", setmealDTO);
        setmealService.updateWithStDish(setmealDTO);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @ApiOperation("分页查询")
    public Result<SetmealVO> getSetmealPage(@PathVariable Long id) {
        log.info("id查询套餐:{}", id);
        SetmealVO SetmealVO = setmealService.getBySetmealId(id);
        return Result.success("", SetmealVO);
    }

    @DeleteMapping()
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> delSetmealPage(@RequestParam List<Long> ids) {
        log.info("删除套餐:{}", ids);
        setmealService.deleteSetmeal(ids);
        return Result.success(null);
    }

    @ApiOperation("启用/禁用售卖状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启/禁员工账号:{},{}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success("操作成功",null);
    }
}
